package com.hunk.springMvcExercise.web.controller;

import static com.hunk.utils.io.Print.println;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * Description:<br>
 * 用于表单中含有文件的上传
 * 
 * @author Hunk
 * @since 2013-4-2
 */
@Controller
@RequestMapping("test/fileform")
public class MutliFileFormController {

	private final String temppath = "E:/temp/uploadFile/";

	/**
	 * @return
	 */
	@RequestMapping(value = "/uploadPage")
	public String uploadPage() {
		return "test/uploadPage";
	}
	
	/** Receive a From, which not only contains text field, but also file field 
	 * @param title
	 * @param descrption
	 * @param file1
	 * @param file2
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addFrom")
	public Response<?> addForm(@RequestParam("title") String title,
			@RequestParam("descrption") String descrption,
			@RequestParam("file1") MultipartFile file1,
			@RequestParam("file2") MultipartFile file2) {
		try {
			String savePath1 = null;
			String savePath2 = null;
			if (!file1.isEmpty()) {
				savePath1 = temppath + getPhotoFileName();
				file1.transferTo(new File(savePath1));
			}
			if (!file2.isEmpty()) {
				savePath2 = temppath + getPhotoFileName();
				file2.transferTo(new File(savePath2));
			}
			Form form = new Form2(title, descrption, savePath1, savePath2);
			println("success");
			println(form);
			return new Response<Form>(true, form);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// spring mvc 内置的jackson 需要set get方法进行序列化，但是奇怪的是不需要继承Serializable接口
		return Response.getTureAndEmptyContentInstance();
	}

	@RequestMapping(value = "/feedback")
	@Deprecated
	public String feedback(HttpSession session, ModelMap mm) {
		mm.addAttribute("baseResponse", Response.getTureAndEmptyContentInstance());
		return "success";
	}
	
	/** 用当前时间给取得的图片命名  */
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyy-MM-dd-HH-mm-ss");
		System.out.println(dateFormat.format(date) + ".jpg");
		return dateFormat.format(date) + ".jpg";
	}

	/**
	 * Description:<br>
	 * Form with text field and one file field
	 * 
	 * @author Hunk
	 * @since 2013-4-2
	 */
	static class Form {
		String title;
		String descrption;
		String file1;

		public Form(String title, String descrption, String file1) {
			super();
			this.title = title;
			this.descrption = descrption;
			this.file1 = file1;
		}

		@Override
		public String toString() {
			return "Form [title=" + title + ", descrption=" + descrption
					+ ", file1=" + file1 + "]";
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getDescrption() {
			return descrption;
		}

		public void setDescrption(String descrption) {
			this.descrption = descrption;
		}

		public String getFile1() {
			return file1;
		}

		public void setFile1(String file1) {
			this.file1 = file1;
		}
		
		
	}

	static class Form2 extends Form {
		String file2;

		public Form2(String title, String descrption, String file1, String file2) {
			super(title, descrption, file1);
			this.file2 = file2;
		}

		@Override
		public String toString() {
			return "Form2 [file2=" + file2 + ", title=" + title
					+ ", descrption=" + descrption + ", file1=" + file1 + "]";
		}

		public String getFile2() {
			return file2;
		}

		public void setFile2(String file2) {
			this.file2 = file2;
		}
		
	}
}

/** Description:<br>
 *  返回体
 * @author Hunk
 * @since 2013-4-3
 * @param <T>
 */
class Response<T> {
	private boolean successed;
	private T Message;	
	public Response(boolean successed, T content) {
		super();
		this.successed = successed;
		this.Message = content;
	}

	public boolean isSuccessed() {
		return successed;
	}

	public void setSuccessed(boolean successed) {
		this.successed = successed;
	}

	public T getMessage() {
		return Message;
	}

	public void setMessage(T message) {
		Message = message;
	}

	/** return a empty Msg Response
	 * @return
	 */
	static Response<?> getTureAndEmptyContentInstance() {
		return new Response<Object>(true, null);
	}
}
